package com.open.pokergames.biz.views.groups.fragments

import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jiangyy.easydialog.CommonDialog
import com.jiangyy.easydialog.InputDialog
import com.open.pokergames.R
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.base.entity.PageReq
import com.open.pokergames.base.fragment.EventBusRecycleViewBaseFragment
import com.open.pokergames.biz.entity.GroupCoinsVo
import com.open.pokergames.biz.entity.GroupMemberListEntity
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.biz.views.groups.GroupCoinsActivity
import com.open.pokergames.biz.views.groups.adapters.GroupMemberOptAdapter
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_fragment_page.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GroupCoinsOptFragment : EventBusRecycleViewBaseFragment<GroupMemberListEntity>(), OnSuperListener<GroupMemberListEntity> {

    private var viewModel: BizViewModel? = null
    private var adapter: GroupMemberOptAdapter? = null
    private var currView: View? = null

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider;
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.comm_fragment_page, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = BizViewModel()
        loadingTipView.setLoadingViewBg(R.color.transparent)
        initRecycleLinearView(loadingRecyclerView)

        adapter = GroupMemberOptAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<GroupMemberListEntity>) {
        val pageReq = PageReq<GroupMemberListEntity>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val data = GroupMemberListEntity()
        data.groupId = GroupCoinsActivity.groupId
        pageReq.data = data
        viewModel!!.pageForGroupMember(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<GroupMemberListEntity>>) {

        if ("pageForGroupMember" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常", R.drawable.data_empty)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView?.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<GroupMemberListEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.data.size == 0) {
                loadingTipView.showEmpty("暂无消息", R.drawable.data_empty)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.data!!)
                loadingTipView?.showContent()
            }
        } else {
            if (appPageData.data.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.data!!)
                loadingTipView?.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: GroupMemberListEntity?, flag: String?) {
        if (itemData == null) {
            XUtils.toast().show("数据异常")
            return
        }

        when (flag) {
            "addTv" -> {
                InputDialog.Builder(activity)
                    .setTitle("发放积分")
                    .setInputType(InputType.TYPE_CLASS_NUMBER)
                    .setHint("请输入发放积分数")
                    .setLines(1)
                    .setPositiveButton(
                        "发放"
                    ) { view ->
                        CommonDialog.Builder(activity)
                            .setCancelable(false)
                            .setTitle("发放积分提示")
                            .setMessage("点击确定后，积分将直接转给对方！确定发放吗？")
                            .setPositiveButton("确定") {
                                showLoadingDialog("正在发放")
                                val vo = GroupCoinsVo()
                                vo.groupId = GroupCoinsActivity.groupId
                                vo.getAccount = itemData.account
                                vo.type = 0
                                vo.getCoins = view.tag.toString()
                                viewModel!!.optGroupCoin(vo)
                            }.setNegativeButton("取消") {
                            }.show()
                    }
                    .setNegativeButton("取消", null)
                    .show()
            }
            "removeTv" -> {
                InputDialog.Builder(activity)
                    .setTitle("收回积分")
                    .setInputType(InputType.TYPE_CLASS_NUMBER)
                    .setHint("请输入收回积分数")
                    .setLines(1)
                    .setPositiveButton(
                        "收回"
                    ) { view ->

                        CommonDialog.Builder(activity)
                            .setCancelable(false)
                            .setTitle("收回积分提示")
                            .setMessage("点击确定后，积分将直接扣除对方积分！确定收回吗？")
                            .setPositiveButton("确定") {

                                showLoadingDialog("正在收回")
                                val vo = GroupCoinsVo()
                                vo.groupId = GroupCoinsActivity.groupId
                                vo.getAccount = itemData.account
                                vo.type = 1
                                vo.getCoins = view.tag.toString()
                                viewModel!!.optGroupCoin(vo)
                            }.setNegativeButton("取消") {
                            }.show()
                    }
                    .setNegativeButton("取消", null)
                    .show()
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun optGroupCoinEventBusCallback(result: ResultInfo<String>) {

        if ("optGroupCoin" != result.eventType) {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                refreshData()
                GroupCoinLogFragment.instant?.refreshData()
//                GroupCoinsActivity.instant?.selectTab(0)
            }
            else -> {
                XUtils.toast().showLong(result.msg)
            }
        }
    }
}